//
//  BGTenderInfoViewController.m
//  bigMall
//
//  Created by LTOVE on 16/8/18.
//  Copyright © 2016年 thinklev. All rights reserved.
//

#import "BGTenderInfoViewController.h"
#import "BGDateResult.h"

#import "BGGoodInfoDataResult.h"

#import "BGGoodInfoFramDataModel.h"

#import "BGShopAddListViewController.h"

#import "BGMessageContentFrameModel.h"
#import "BGAuctionDetailsDataModel.h"
#import "BGGetAddListDataModel.h"
#import "BGGoodInfoResult.h"
#import "BGAddCollectModel.h"
#import "LTOVEDynamicCreakButton.h"
#import "BGCheckCollect.h"
#import "BGLoginViewController.h"

#import "BGSellerGodBidController.h"

#import "BGSellerGoBidParam.h"
#import "BGAuctionLogListModel.h"
#import "MJExtension.h"

#import "MBProgressHUD+MJ.h"
#import "MBProgressHUD.h"
#import <UITableView_FDTemplateLayoutCell/UITableView+FDTemplateLayoutCell.h>
#import "BGMessageBasicCell.h"
@interface BGTenderInfoViewController (){
    
    NSArray *_addListArray;//立即下单数据源数组
    NSString *_num;
    NSString *_leastNum;
    NSArray *_collBtnArray;
    NSString *price;
    NSMutableArray *oneDataArray;
    NSMutableArray *twoDataArray;
    NSInteger _thisPage;
    NSInteger _allPage;
    NSString *maginScal;
}


@end

@implementation BGTenderInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
////    self.tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
//    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [BGDateResult checkCollectWithType:@"invite" objId:self.goodId success:^(NSObject *result) {
        BGCheckCollect *model = (BGCheckCollect *)result;
        NSLog(@"%@",model.status_str);
        self.cancelID = model.idStr;
        if ([model.status_str isEqualToString:@"收藏"]) {
            self.isRect = NO;
            [self.views setTitle:@"收藏" forState:UIControlStateNormal index:0];
        }else if([model.status_str isEqualToString:@"取消收藏"]){
            self.isRect = YES;
            [self.views setTitle:@"取消收藏" forState:UIControlStateNormal index:0];
        }else
            return ;
    } failure:^(NSError *error) {
        
        
    }];
    
    // Do any additional setup after loading the view.
    self.title = @"报价";
    
    
    self.tableView.fd_debugLogEnabled = YES;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setGoodId:(NSString *)goodId
{
    _goodId = goodId;
    [BGDateResult loadinviteId:goodId success:^(NSObject *result) {
        NSArray *arr = [BGGoodInfoFramDataModel getDataArrayWithBgTenderInfoResult:(BGGoodInfoResult *)result];
        
        
        
//        [BGDateResult bidListWithInvitedId:self.goodId thisPage:@"1" pageNum:@"10" success:^(NSObject *result) {
//            BGAuctionLogListModel *resultModel = (BGAuctionLogListModel *)result;
//            NSLog(@"%@",resultModel.keyValues);
//            
//        } failure:^(NSError *error) {
//            
//            
//        }];
        self.dataArray = arr;
        BGGoodInfoResult *mmm = (BGGoodInfoResult *)result;
        _num = mmm.number;
        _leastNum = mmm.least_number;
        maginScal = mmm.margin_scale;
        price = mmm.price;
        //        [arr writeToFile:@"/Users/LTOVE/Desktop/123xc.plist" atomically:YES];
        //        GWLog(@"%@",arr);
//        [self.tableView reloadData];
        _addListArray = [BGGetAddListDataModel getDataArrayWithBGGoodInfoResult:(BGGoodInfoResult *)result];
        
    } failure:^(NSError *error) {
        
        
    }];

#pragma mark - 引入多线程
    dispatch_group_t group = dispatch_group_create();
    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
    
    dispatch_group_async(group, queue, ^{
        dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
        [BGDateResult loadinviteId:goodId success:^(NSObject *result) {
            NSArray *arr = [BGGoodInfoFramDataModel getDataArrayWithBgTenderInfoResult:(BGGoodInfoResult *)result];
            oneDataArray = [NSMutableArray arrayWithArray:arr];
            
            dispatch_semaphore_signal(semaphore);
            
        } failure:^(NSError *error) {
            dispatch_semaphore_signal(semaphore);
        }];
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
        
        
    });
    
    dispatch_group_async(group, queue, ^{
        dispatch_semaphore_t semphore = dispatch_semaphore_create(0);
        [BGDateResult bidListWithInvitedId:self.goodId thisPage:@"1" pageNum:@"10" success:^(NSObject *result) {
            BGAuctionLogListModel *model = (BGAuctionLogListModel *)result;
            NSArray *array = [BGGoodInfoFramDataModel getDataArrayWithBGAuctionInfoMessageList:model];
            
            twoDataArray = [NSMutableArray arrayWithArray:array];
//            while (twoDataArray) {
//                dispatch_semaphore_signal(semphore);
//            }
            dispatch_semaphore_signal(semphore);
        } failure:^(NSError *error) {
            dispatch_semaphore_signal(semphore);
        }];
        dispatch_semaphore_wait(semphore, DISPATCH_TIME_FOREVER);
        
    });
    
    dispatch_group_notify(group, queue, ^{
        
        NSLog(@"%lu---%lu>>%@---%@",(unsigned long)oneDataArray.count,(unsigned long)twoDataArray.count,oneDataArray,twoDataArray);
        NSMutableArray *dataArray = [NSMutableArray array];
        if (oneDataArray.count > 0) {
            [dataArray addObjectsFromArray:oneDataArray];
        }
        if (twoDataArray.count > 0) {
            [dataArray addObjectsFromArray:twoDataArray];
        }
        
        self.dataArray = dataArray;
        [self.tableView reloadData];
        self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
        [self.tableView.header beginRefreshing];
    });
    
#pragma mark - ss
}



#pragma mark- 刷新加载
- (void)loadData{
    
    NSLog(@"self.goodId === %@",self.goodId);
    [MBProgressHUD showMessage:@"加载中..."];
    [BGDateResult bidListWithInvitedId:self.goodId thisPage:@"1" pageNum:@"10" success:^(NSObject *result) {
        [self.tableView.header endRefreshing];
        [MBProgressHUD hideHUD];
        BGAuctionLogListModel *model = (BGAuctionLogListModel *)result;
        NSArray *array = [BGGoodInfoFramDataModel getDataArrayWithBGAuctionInfoMessageList:model];
        
        twoDataArray = [NSMutableArray arrayWithArray:array];
        NSMutableArray *dataArray = [NSMutableArray array];
        if (oneDataArray.count > 0) {
            [dataArray addObjectsFromArray:oneDataArray];
        }
        if (twoDataArray.count > 0) {
            [dataArray addObjectsFromArray:twoDataArray];
        }
        
        self.dataArray = dataArray;
        [self.tableView reloadData];
        if ([model.allPage integerValue] > 10) {
            self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
            _thisPage = 2;
            _allPage = [model.allPage integerValue];
        }
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUD];
    }];
}
- (void)loadMoreData
{
    if (_thisPage > _allPage) {
        [self.tableView.footer endRefreshingWithNoMoreData];
        return;
    }
    [MBProgressHUD showMessage:@"加载中..."];
    [BGDateResult bidListWithInvitedId:self.goodId thisPage:[NSString stringWithFormat:@"%ld",(long)_thisPage] pageNum:@"10" success:^(NSObject *result) {
        [MBProgressHUD hideHUD];
        BGAuctionLogListModel *model = (BGAuctionLogListModel *)result;
        NSArray *array = [BGGoodInfoFramDataModel getDataArrayWithBGAuctionInfoMessageList:model];
        NSMutableArray *dataArrays = [NSMutableArray arrayWithArray:self.dataArray];
        [dataArrays addObjectsFromArray:array];
        self.dataArray = dataArrays;
        [self.tableView reloadData];
        if ((model.list.count)) {
            _thisPage ++;
        }
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUD];
    }];
}

#pragma mark -




- (void)dynamicButtonClick:(UIButton *)btn
{
    switch (btn.tag) {
        case 0:
            GWLog(@"点击了收藏");
            [self collectBtnClick];
            break;
        case 1:
            GWLog(@"点击了下单");
            [self pushToShopAddListController];
            break;
            
        default:
            break;
    }
}

- (void)collectBtnClick
{
    if (!self.isRect) {
        [self addCollection];
    }else
        [BGDateResult checkCollectWithType:@"invite" objId:self.goodId success:^(NSObject *result) {
            BGCheckCollect *model = (BGCheckCollect *)result;
            NSLog(@"````````````%@",model.idStr);
            self.cancelID = model.idStr;
            [self cancelCollection];
            
        } failure:^(NSError *error) {
            
            
        }];
    
}

- (void)cancelCollection
{
    [BGDateResult cancelCollectWithId:self.cancelID success:^(NSObject *result) {
        BGAddCollectModel *model = (BGAddCollectModel *)result;
        NSLog(@"%@",model.status);
        UIAlertController *alertView = [UIAlertController alertControllerWithTitle:@"提示" message:model.msg preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
            if ([model.status isEqualToString:@"1"]) {
                self.isRect = NO;
                [self.views setTitle:@"收藏" forState:UIControlStateNormal index:0];
                
            }
            //            self.btnArray = @[@"已收藏",@"立即下单"];
        }];
        
        [alertView addAction:cancel];
        
        [self presentViewController:alertView animated:YES completion:^{
            
            
        }];
    } failure:^(NSError *error) {
        
        
    }];
    
}
- (void)addCollection
{
    [BGDateResult addCollectWithType:@"invite" objId:self.goodId success:^(NSObject *result) {
        BGAddCollectModel *model = (BGAddCollectModel *)result;
        UIAlertController *alertView = [UIAlertController alertControllerWithTitle:@"提示" message:model.msg preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            if ([model.status isEqualToString:@"1"] && !self.isRect) {
                self.isRect = YES;
                [self.views setTitle:@"取消收藏" forState:UIControlStateNormal index:0];
            }else if ([model.status isEqualToString:@"1"] && self.isRect){
                self.isRect = NO;
                [self.views setTitle:@"收藏" forState:UIControlStateNormal index:0];
            }else if([model.status isEqualToString:@"-2"]){
                BGLoginViewController *vc = [[BGLoginViewController alloc]init];
                
                [self.navigationController pushViewController:vc animated:YES];
            }else if ([model.status isEqualToString:@"-3"] && [model.msg isEqualToString:@"您已经收藏过"]){
                self.isRect = YES;
                [self.views setTitle:@"取消收藏" forState:UIControlStateNormal index:0];
            }else
                return ;
            
            //            self.btnArray = @[@"已收藏",@"立即下单"];
        }];
        
        [alertView addAction:cancel];
        
        [self presentViewController:alertView animated:YES completion:^{
            
            
        }];
        
    } failure:^(NSError *error) {
        
        
    }];
    
}

//- (void)alertViewWith

- (void)pushToShopAddListController
{
    [BGDateResult getUserInfoSuccess:^(NSString *status, NSString *msg, NSObject *res) {
        if ([status intValue] == -2) {
            UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"提示" message:@"需要登录" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *sure = [UIAlertAction actionWithTitle:@"去登录" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                BGLoginViewController *vc = [BGLoginViewController new];
                [self.navigationController pushViewController:vc animated:YES];
                
            }];
            [alertVc addAction:sure];
            [self presentViewController:alertVc animated:YES completion:^{
                
                
            }];
        }else{
            NSArray *ara = _addListArray;
            //    GWLog(@"%@",ara);  BGSellerGodBidController
            //    BGShopAddListViewController *vc = [BGShopAddListViewController initAddListviewWithDataArray:ara];
            //    BGSellerGodBidController *vc = [BGSellerGodBidController initAddListviewWithDataArray:ara];
            BGSellerGodBidController *vc = [BGSellerGodBidController new];
            //    vc.goodId = self.goodId;
            //    //    NSLog(@"%@",self.goodId);
            //    vc.Num = _num;
            //    vc.leastNum = _leastNum;
            BGSellerGoBidParam *model = [BGSellerGoBidParam new];
            model.number = _leastNum;
            model.inviteId = self.goodId;
            model.price = price;
            model.MaginScal = maginScal;
            vc.navigationItem.title = @"报价";
            vc.model = model;
            vc.hidesBottomBarWhenPushed = YES;
            UIBarButtonItem *back = [[UIBarButtonItem alloc] init];
            back.title = @"";
            self.navigationItem.backBarButtonItem = back;
            [self.navigationController pushViewController:vc animated:YES];

        }
    } failure:^(NSError *error) {
        
        
    }];
    
}
- (void)viewWillAppear:(BOOL)animated
{
    //    [BGDateResult checkCollectWithType:@"goods" objId:self.goodId success:^(NSObject *result) {
    //        BGCheckCollect *model = (BGCheckCollect *)result;
    //        if ([model.status_str isEqualToString:@"收藏"]) {
    //            self.isRect = YES;
    //            self.btnArray = @[@"收藏",@"立即下单"];
    //        }else{
    //            self.isRect = NO;
    //            self.btnArray = @[@"取消收藏",@"立即下单"];
    //        }
    //    } failure:^(NSError *error) {
    //
    //
    //    }];
    self.btnArray = @[@"收藏",@"立即报价"];
    self.isRect = NO;
    NSLog(@"%@",_collBtnArray);
    //    self.btnArray = _collBtnArray;
    [super viewWillAppear:animated];
}


//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    BGMessageContentFrameModel *Fmodel = self.dataArray[indexPath.row];
//    
//    if ([Fmodel isKindOfClass:[BGAuctionDetailsDataModel class]]) {
//        NSLog(@"*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-%f",[tableView fd_heightForCellWithIdentifier:@"cell" configuration:^(BGMessageBasicCell * cell) {
//            cell.Fmodel = Fmodel;
//        }]);
//        return [tableView fd_heightForCellWithIdentifier:@"cell" configuration:^(BGMessageBasicCell * cell) {
//            cell.Fmodel = Fmodel;
//        }];
//    }else{
//        return Fmodel.cellHeigh;
//    }
//    
//}


@end
